﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Diagnostics;

namespace CommonLibrary
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// NLog 实例对象
        /// </summary>
        private static NLog.ILogger LOG = NLog.LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 记录MES日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Mes(string msg)
        {
            StackTrace stackTrace = new StackTrace();
            string type = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
            string method = stackTrace.GetFrame(1).GetMethod().Name.ToString();
            msg = $"【位置：{type}-{method}】\t{msg}";
            LOG.Trace(msg);
        }
        /// <summary>
        /// 记录INFO日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(string msg)
        {
            StackTrace stackTrace = new StackTrace();
            string type = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
            string method = stackTrace.GetFrame(1).GetMethod().Name.ToString();
            msg = $"【位置：{type}-{method}】\t{msg}";
            LOG.Info(msg);
        }
        /// <summary>
        /// 记录DEBUG日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="tag"></param>
        public static void Debug(string msg, string tag = "")
        {
            StackTrace stackTrace = new StackTrace();
            string type = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
            string method = stackTrace.GetFrame(1).GetMethod().Name.ToString();
            tag = string.IsNullOrWhiteSpace(tag) ? $"{type}-{method}" : tag;
            msg = $"【位置：{tag}】\t{msg}";
            LOG.Debug(msg);
        }
        /// <summary>
        /// 记录WARN日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="tag"></param>
        public static void Warn(string msg, string tag = "")
        {
            StackTrace stackTrace = new StackTrace();
            string type = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
            string method = stackTrace.GetFrame(1).GetMethod().Name.ToString();
            tag = string.IsNullOrWhiteSpace(tag) ? $"{type}-{method}" : tag;
            msg = $"【位置：{tag}】\t{msg}";
            LOG.Warn(msg);
        }
        /// <summary>
        /// 记录ERROR日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="tag"></param>
        /// <param name="otherMessage"></param>
        public static void Error(Exception ex, string tag = "", string otherMessage = "")
        {
            StackTrace stackTrace = new StackTrace();
            string type = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
            string method = stackTrace.GetFrame(1).GetMethod().Name.ToString();
            tag = string.IsNullOrWhiteSpace(tag) ? $"{type}-{method}" : tag;
            if (ex != null)
            {
                LOG.Error($"{(string.IsNullOrWhiteSpace(tag) ? "" : $"【位置：{tag}】 {otherMessage}")}\t【异常信息】{ex.Message}\t【堆栈调用】{ex.StackTrace}\t【异常方法】{ex.TargetSite}");
            }
            else
            {
                LOG.Error($"{(string.IsNullOrWhiteSpace(tag) ? "" : $"【位置：{tag}】 {otherMessage}")}\t【非法异常信息】");
            }
        }
    }
}
